package com.sunlg.smarthome.ui.personal;

import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.sunlg.smarthome.R;

/**
 * Created by sunlg on 2017/5/30.
 */

public class UpdateMsgDialog {
    private Context context;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    private View view;
    private TextInputEditText editText;

    private UpdateMsgDialog.OnEditTextInputListener editTextInputListener;

    public UpdateMsgDialog(Context context) {
        this.context = context;

    }


    public void show(String title, String hint){

        builder = new AlertDialog.Builder(context);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_with_edittext1, null);
        editText = (TextInputEditText)view.findViewById(R.id.editText);

        if(!TextUtils.isEmpty(hint)){
            editText.setHint(hint);
        }

        if(view != null) {
            builder.setView(view);
        }
        if(!TextUtils.isEmpty(title)){
            builder.setTitle(title);
        }
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                String input = editText.getText().toString();

                editTextInputListener.onEditTextInput(input);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    public void hide(){
        if(dialog != null){
            dialog.dismiss();
        }
    }

    public AlertDialog.Builder getBuilder(){
        return builder;
    }

    public void setHint(String hint){
        editText.setHint(hint);
    }


    public boolean isShowing(){
        return dialog.isShowing();
    }

    public interface OnEditTextInputListener{
        void onEditTextInput(String input);
    }

    public void setOnEditTextInputListener(UpdateMsgDialog.OnEditTextInputListener listener){
        editTextInputListener = listener;
    }
}
